<?php

namespace App\Http\Controllers;

use Laravel\Lumen\Routing\Controller as BaseController;
use App\Http\Service\OrderService;
use App\Http\Service\CommentService;
use App\Http\Service\GoodsCommentService;
use App\Http\Service\PointLogService;
use App\Http\Service\ActivityService;
use Illuminate\Http\Request;

class CommentController extends BaseController
{

    protected $request;
    protected $user;
    protected $orderService;
    protected $goodsCommentService;
    protected $pointLogService;
    protected $activityService;


    public function __construct(Request $request,
                                OrderService $orderService,
                                GoodsCommentService $goodsCommentService,
                                PointLogService $pointLogService,
                                ActivityService $activityService)
    {
        $this->request = $request;
        $this->user = $this->request->get('user');
        $this->orderService = $orderService;
        $this->goodsCommentService = $goodsCommentService;
        $this->pointLogService = $pointLogService;
        $this->activityService = $activityService;
    }

    public function activityAdd()
    {
        $id = $this->request->input('id', 0);
        $content = $this->request->input('content', '');

        $activity = $this->activityService->find($id);
        if ($activity == null) {
            return toJSON('', \Constant::FAILED, '活动不存在');
        }

        if (strlen($content) < 5) {
            return toJSON('', \Constant::NEED_SHOW_MESSAGE, '评论内容不少于5个字');
        }

        $comment = $this->goodsCommentService->create([
            'user_id' => $this->user->id,
            'goods_id' => $activity->goods_id,
            'content' => $content,
            'is_active' => 1,
        ]);

        $u = array();
        $u['user_id'] = $this->user->user_id;
        $u['nickname'] = $this->user->nickname;
        $u['avatar'] = $this->user->avatar;

        $comment['user'] = $u;
        $comment['images'] = array();

        return toJSON($comment);
    }

    // 订单评论
    public function orderAdd()
    {
        $id = $this->request->input('id', 0);
        $content = $this->request->input('content', '');

        if (strlen($content) < 5) {
            return toJSON('', \Constant::NEED_SHOW_MESSAGE, '评论内容不少于5个字');
        }

        $order = $this->orderService->findFirstRowByFields(['id' => $id, 'user_id' => $this->user->id]);

        if ($order == null) {
            return toJSON('', \Constant::NEED_SHOW_MESSAGE, '订单不存在');
        }

        if ($order->is_commented == 1) {
            return toJSON('', \Constant::NEED_SHOW_MESSAGE, '请勿重复评论');
        }

        $point = 5;
        //用户积分改变
        $this->user->history_points = $this->user->history_points + $point;
        $this->user->points = $this->user->points + $point;
        $this->user->save();

        //积分日志
        $this->pointLogService->create([
            'user_id' => $this->user->id,
            'number' => $point,
            'change_desc' => '评论' . $order->goods->name,
            'change_type' => 5,
        ]);

        $this->goodsCommentService->create([
            'user_id' => $this->user->id,
            'goods_id' => $order->goods_id,
            'content' => $content,
            'is_active' => 1,
        ]);

        $order->is_commented = 1;
        $order->save();

        return toJSON();
    }

    // 用户评论列表
    public function my() {

        $page = $this->request->input('page', 1);
        $user = $this->request->get('user');

        $comments = $this->goodsCommentService->getMyList($user, $page);
        return toJSON($comments);
    }

    // 活动评论列表
    public function activity() {

        $page = $this->request->input('page', 2);
        $id = $this->request->input('id', 0);

        $activity = $this->activityService->find($id);
        if ($activity == null) {
            return toJSON('', \Constant::FAILED, '活动不存在');
        }

        $comments = $this->goodsCommentService->getList($activity->goods_id, $page);
        return toJSON($comments);
    }
}
